<template>
  <div>
    <div class="card" style="margin-bottom: 5px;">
      <el-input v-model="name" style="width: 300px; margin-right: 10px" placeholder="请输入名称查询"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <el-table :data="tableData" stripe>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="头像">
          <template slot-scope="scope">
            <el-image :src="scope.row.avatar" style="width: 40px; height: 40px; border-radius: 50%"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="角色" prop="role">
          <template slot-scope="scope">
            <span v-if="scope.row.role === 'ADMIN'">管理员</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card">
      <el-pagination background layout="prev, pager, next" :page-size="pageSize" :current-page="pageNum" :total="total"
        @current-change="handlePageChange">
      </el-pagination>
    </div>

    <el-dialog title="信息" width="40%" :visible.sync="formVisible" :close-on-click-modal="false">
      <el-form :model="form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="头像" prop="avatar">
          <el-upload :action="uploadUrl" list-type="picture" :on-success="handleImgSuccess">
            <el-button type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import { MessageBox, Message } from "element-ui";

export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      total: 0,
      formVisible: false,
      form: {},
      tableData: [],
      name: null,
      uploadUrl: process.env.VITE_BASE_URL + '/files/upload'
    }
  },
  methods: {
    load() {
      request.get('/admin/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res => {
        this.tableData = res.data?.list || []
        this.total = res.data?.total || 0
      })
    },
    handleAdd() {
      this.form = {}
      this.formVisible = true
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.formVisible = true
    },
    add() {
      request.post('/admin/add', this.form).then(res => {
        if (res.code === '200') {
          this.load()
          Message.success('操作成功')
          this.formVisible = false
        } else {
          Message.error(res.msg)
        }
      })
    },
    update() {
      request.put('/admin/update', this.form).then(res => {
        if (res.code === '200') {
          this.load()
          Message.success('操作成功')
          this.formVisible = false
        } else {
          Message.error(res.msg)
        }
      })
    },
    save() {
      this.form.id ? this.update() : this.add()
    },
    handleDelete(id) {
      MessageBox.confirm('删除后数据无法恢复，您确定删除吗?', '删除确认', { type: 'warning' }).then(() => {
        request.delete('/admin/delete/' + id).then(res => {
          if (res.code === '200') {
            this.load()
            Message.success('操作成功')
          } else {
            Message.error(res.msg)
          }
        })
      }).catch(() => { })
    },
    reset() {
      this.name = null
      this.load()
    },
    handleImgSuccess(res) {
      this.form.avatar = res.data
    },
    handlePageChange(val) {
      this.pageNum = val
      this.load()
    }
  },
  created() {
    // this.load()
  }
}
</script>